<template>
  <div class = "login_container">
    <div class="login_box">
      <!--      头像区域-->
      <div class="avatar_box">
        <img src="../../assets/logo.png" alt="">
        <!--          <el-image :src="require('@/assets/logo.png')"></el-image>-->
      </div>
      <!--      登录表单区域-->
      <el-form label-width="0px" class="login_form" ref="formRef" :model="form" :rules="rules">
        <el-form-item prop="username">
          <el-input prefix-icon="el-icon-user" v-model="form.username"></el-input>
        </el-form-item>
        <el-form-item prop="password">
          <el-input prefix-icon="el-icon-lock" v-model="form.password" type="password"></el-input>
        </el-form-item>
        <el-form-item prop="code">
          <el-input prefix-icon="el-icon-arrow-right" v-model="form.code" style="width: 68%; float: left"></el-input>
          <el-image :src="captchaImg" style="float: left; margin-left: 8px; border-radius: 4px" @click="getCaptcha"></el-image>
        </el-form-item>
        <el-form-item class="btns">
          <el-button type="primary" @click="login">登录</el-button>
          <el-button type="info" @click="resetForm">重置</el-button>
        </el-form-item>
      </el-form>
    </div>
  </div>
</template>

<script>
import qs from 'qs'
import { getCaptchaImg } from '@/api/login'
export default {
  data() {
    return {
      form: {
        username: '',
        password: '',
        code: '',
        keyCode: ''
      },
      rules: {
        username: [
          { require: true, message: '请输入用户名', trigger: 'blur'},
          { min: 5, max: 15, message: '输入字符长度至少3个，至多15个', trigger: 'blur'}
        ],
        password: [
          { require: true, message: '请输入密码', trigger: 'blur'},
          { min: 6, max: 15, message: '输入字符长度至少6个，至多15个', trigger: 'blur'}
        ],
        code: [
          { require: true, message: '请输入验证码', trigger: 'blur'},
          { min: 5, max: 5, message: '输入的验证码长度是5位', trigger: 'blur'}
        ]
      },
      captchaImg: null
    };
  },
  methods: {
    resetForm() {
      // console.log(this)
      this.$refs.formRef.resetFields()
    },
    login() {
      //表单预验证
      this.$refs.formRef.validate(async valid => {
        // console.log(valid)
        if (!valid) return ;
        // const result = await this.$axios.post('login', this.form);  可以直接获取data属性
        // const {data: res} = await this.$axios.post('login', this.form);
        // console.log(res)
        // if (res.meta.status != 200) return this.$message.error("登录失败")
        // this.$message.success("登录成功")
        //将token保存到客户端的sessionStorage里
        // window.sessionStorage.setItem("token", res.data.token)
        //通过编程式导航跳转到后台主页
        // await this.$router.push("/home");
        this.$axios.post("/login?"+ qs.stringify(this.form)).then(res => {
          this.$message.success("登录成功!")
          const jwt = res.headers['authorization']
          this.$store.commit('SET_TOKEN', jwt)
          this.$router.push("/index");
        })
      })
    },
    getCaptcha() {
      getCaptchaImg().then(res =>{
        // console.log("/captcha")
        // console.log(res)
        this.form.keyCode = res.data.data.keyCode
        this.captchaImg = res.data.data.captchaImg
        this.form.code = ''
      })
      // this.$axios.get("/captcha").then(res =>{
      //   console.log("/captcha")
      //   console.log(res)
      //   this.form.keyCode = res.data.data.keyCode
      //   this.captchaImg = res.data.data.captchaImg
      //   this.form.code = ''
      // })
    },
  },
  created() {
    this.getCaptcha()
  }
};
</script>

<style lang="less" scoped>
.login_container{
  background: #2b4b6b;
  height: 100%;
}
.login_box{
  width: 450px;
  height: 350px;
  background-color: #fff;
  border-radius: 3px;
  position: absolute;
  left: 50%;
  top: 50%;
  transform: translate(-50%, -50%);

  .avatar_box{
    height: 130px;
    width: 130px;
    border: 1px solid #eee;
    border-radius: 50%;
    padding: 10px;
    box-shadow: 0 0 10px #ddd;
    position: absolute;
    left: 50%;
    transform: translate(-50%, -50%);
    background-color: #fff;
    img{
      width: 100%;
      height: 100%;
      border-radius: 50%;
      background-color: #eee;
    }
  }
}

.login_form{
  position: absolute;
  bottom: 0;
  width: 100%;
  padding: 0 20px;
  box-sizing: border-box;
}

.btns{
  display: flex;
  justify-content: flex-end;
}
</style>